package com.pixiv.controller;

import cn.hutool.json.JSONObject;
import com.pixiv.Entity.User;
import com.pixiv.service.CoverShowService;
import com.pixiv.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("covershow")
public class CoverShowController {

    @Autowired
    private CoverShowService coverShowService;
    @Autowired
    private UserService userService;

    @RequestMapping("showCoverShow")
    public void showCoverShow(HttpServletRequest req, HttpServletResponse resp)
    {
        HttpSession hs = req.getSession();
        User user = (User) hs.getAttribute("GLOBAL_USER_OBJECT");
        System.out.println(user.getVar().get_vector("GLOBAL_Select_User_ID"));
        User selectUser = userService.getUser((Integer) user.getVar().get_vector("GLOBAL_Select_User_ID"));
        //User selectUser = User.getUser((Integer) user.getVar().get_vector("GLOBAL_Select_User_ID"));
        if (coverShowService.isExistUser(selectUser.getUserID())) {
            String path = coverShowService.getCover(selectUser.getUserID());
            User.outPutPictureStream(resp, path, null, null);
        }else
        {
            User.outPutPictureStream(resp, "user\\init\\covershow.jpg", null, null);
        }
    }

    @ResponseBody
    @RequestMapping("changeCoverShow")
    public String changeCoverShow(HttpServletRequest req, HttpServletResponse resp)
    {
        HttpSession hs = req.getSession();
        User user = (User) hs.getAttribute("GLOBAL_USER_OBJECT");
        User selectUser = userService.getUser((Integer) user.getVar().get_vector("GLOBAL_Select_User_ID"));
        //User selectUser = User.getUser((Integer) user.getVar().get_vector("GLOBAL_Select_User_ID"));

        JSONObject jsonObject = new JSONObject();

        if (coverShowService.isExistUser(selectUser.getUserID())) {
            jsonObject.accumulate("posX", coverShowService.getXPos(selectUser.getUserID()));
            jsonObject.accumulate("posY", coverShowService.getYPos(selectUser.getUserID()));
        }else{
            jsonObject.accumulate("posX", 0);
            jsonObject.accumulate("posY", -200);
        }
        return jsonObject.toString();
    }

}
